import request from '@/utils/request'

// 仪表盘
export function getDashboardStats() {
  return request({
    url: '/api/admin/dashboard/stats',
    method: 'get'
  })
}

export function getLatestOrders() {
  return request({
    url: '/api/admin/dashboard/latest-orders',
    method: 'get'
  })
}

// 用户管理
export function getUserList(params) {
  return request({
    url: '/api/admin/users',
    method: 'get',
    params
  })
}

export function updateUserStatus(id, status) {
  return request({
    url: `/api/admin/users/${id}/status`,
    method: 'put',
    params: { status }
  })
}

export function deleteUser(id) {
  return request({
    url: `/api/admin/users/${id}`,
    method: 'delete'
  })
}

export function createUser(data) {
  return request({
    url: '/api/admin/users',
    method: 'post',
    data
  })
}

export function updateUser(id, data) {
  return request({
    url: `/api/admin/users/${id}`,
    method: 'put',
    data
  })
}

// 宠物管理
export function getPetList(params) {
  return request({
    url: '/api/pets/admin/list',
    method: 'get',
    params
  })
}

export function getPetDetail(id) {
  return request({
    url: `/api/pets/${id}`,
    method: 'get'
  })
}

export function createPet(data) {
  return request({
    url: '/api/pets/admin/create',
    method: 'post',
    data
  })
}

export function updatePet(id, data) {
  return request({
    url: `/api/pets/admin/${id}`,
    method: 'put',
    data
  })
}

export function deletePet(id) {
  return request({
    url: `/api/pets/admin/${id}`,
    method: 'delete'
  })
}

export function updatePetImage(id, file) {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: `/api/pets/admin/${id}/image`,
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 订单管理
export function getOrderList(params) {
  return request({
    url: '/api/admin/orders',
    method: 'get',
    params
  })
}

export function getOrderDetail(id) {
  return request({
    url: `/api/admin/orders/${id}`,
    method: 'get'
  })
}

export function updateOrderStatus(id, status) {
  return request({
    url: `/api/admin/orders/${id}/status`,
    method: 'put',
    params: { status }
  })
}

export function deleteOrder(id) {
  return request({
    url: `/api/admin/orders/${id}`,
    method: 'delete'
  })
}

// 服务管理
export function getServiceList(params) {
  return request({
    url: '/api/admin/services',
    method: 'get',
    params
  })
}

export function createService(data) {
  return request({
    url: '/api/admin/services',
    method: 'post',
    data
  })
}

export function updateService(id, data) {
  return request({
    url: `/api/admin/services/${id}`,
    method: 'put',
    data
  })
}

export function deleteService(id) {
  return request({
    url: `/api/admin/services/${id}`,
    method: 'delete'
  })
}

// 更新服务状态的专用函数
export function updateServiceStatus(id, status) {
  return request({
    url: `/api/admin/services/${id}/status`,
    method: 'put',
    params: { status }
  })
}

// 预约管理
export function getAppointmentList(params) {
  return request({
    url: '/api/admin/appointments',
    method: 'get',
    params
  })
}

export function getAppointmentDetail(id) {
  return request({
    url: `/api/admin/appointments/${id}`,
    method: 'get'
  })
}

export function updateAppointmentStatus(id, status) {
  return request({
    url: `/api/admin/appointments/${id}/status`,
    method: 'put',
    params: { status }
  })
}


export function updateSystemSettings(data) {
  return request({
    url: '/api/admin/settings',
    method: 'put',
    data
  })
}

// 商品管理
export function getProductList(params) {
  return request({
    url: '/api/products',
    method: 'get',
    params
  })
}

export function createProduct(data) {
  return request({
    url: '/api/products',
    method: 'post',
    data
  })
}

export function updateProduct(id, data) {
  return request({
    url: `/api/products/${id}`,
    method: 'put',
    data
  })
}

export function deleteProduct(id) {
  return request({
    url: `/api/products/${id}`,
    method: 'delete'
  })
}

export function uploadProductImage(id, file) {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: `/api/products/${id}/image`,
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 通用文件上传
export function uploadFile(data, config) {
  console.log('调用上传接口，数据:', data);
  if (data instanceof FormData) {
    console.log('传入FormData格式，包含文件:', data.get('file'));
    console.log('目标目录:', data.get('directory'));
  }
  
  return request({
    url: '/api/files/upload',
    method: 'post',
    data,
    ...config,
    // 允许跟踪上传进度
    onUploadProgress: progressEvent => {
      const percentCompleted = Math.round(
        (progressEvent.loaded * 100) / progressEvent.total
      );
      console.log('上传进度:', percentCompleted, '%');
    }
  }).catch(error => {
    console.error('上传请求发生错误:', error);
    throw error;
  });
} 